# system_code.py
# 系统核心逻辑：用户登录验证、权限校验、菜单获取
from typing import Optional

# 模拟数据库
USERS = {
    "user001": {"password": "pw001", "permission_code": "600"},
    "user002": {"password": "pw002", "permission_code": "777"},
    "user003": {"password": "pw003", "permission_code": ""},
}

AUTHORIZATIONS = {
    "600": ["001", "002"],
    "777": ["001", "002", "003"]
}

MENUS = {
    "001": {"name": "商品管理", "parent_id": None},
    "002": {"name": "商品录入管理", "parent_id": "001"},
    "003": {"name": "供应商管理", "parent_id": None}
}

def authenticate_user(username: str, password: str) -> Optional[str]:
    user = USERS.get(username)
    if user and user["password"] == password:
        return user["permission_code"]
    return None

def get_user_menus(permission_code: str):
    menu_ids = AUTHORIZATIONS.get(permission_code, [])
    return [MENUS[menu_id] for menu_id in menu_ids if menu_id in MENUS]

if __name__ == "__main__":
    # 示例：登录 user002
    username, password = "user002", "pw002"
    perm = authenticate_user(username, password)
    if perm:
        print(f"{username} 登录成功，权限码: {perm}")
        menus = get_user_menus(perm)
        for menu in menus:
            print(f"菜单: {menu['name']} (父级: {menu['parent_id']})")
    else:
        print("登录失败，用户名或密码错误或无权限")
